// functionPage/pages/secretary/questionInfo/questionInfo.js
import throttle from '../../../../utils/throttle.js';
import { giveLikeApi } from '../../../../utils/tools.js';
import Http from '../../../../utils/http.js';
const http = new Http();
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        navbtn: '', //navbtn  01--操作记录进入-显示顶部导航
        id: '', //问题Id
        review: '',
        answerId: '', //回答id
        answerEdit: false, //回答再次编辑
        answerEditCont: '', //回答再次编辑内容
        showAnswerBox: false,
        answerDescription: '',
        heat: 0,
        // 问题描述是否折叠
        fold: false,
        // 更多操作
        show: false,
        actions: [
            {
                name: '设为优秀答案',
                color: '#333333',
                className: 'select-choice',
            },
            {
                name: '编辑',
                color: '#333333',
                className: 'select-edit',
            },
            {
                name: '删除',
                color: '#ff455e',
                className: 'select-delete',
            },
        ],
        //问题内容
        cityQuestionList: {},
        // 操作内容发布人uid
        operateUid: '',
        anonymous: false, //评论匿名
        answerBoxBottom: 0,
    },
    // 查看他人主页
    jumpOthersPage(e) {
        if (e.currentTarget.dataset.anonymousflg != '01') {
            wx.navigateTo({
                url: `/functionPage/pages/othersHomePage/othersHomePage?uid=${e.currentTarget.dataset.uid}`,
            });
        }
    },
    onLoad(options) {
        this.setData({
            id: options.id,
            review: options.review,
            navbtn: options.navbtn, //navbtn  01--操作记录进入
        });
        this.getCityQuestionList();
    },
    onShow() {},
    refresh() {
        this.getCityQuestionList();
    },

    //点赞问题
    giveLike(e) {
        let _that = this;
        let content = e.currentTarget.dataset.content;
        let praisecnt = 'cityQuestionList.praisecnt';
        let praiseflg = 'cityQuestionList.praiseflg';
        _that.setData({
            [praisecnt]:
                content.praiseflg == '00'
                    ? content.praisecnt + 1
                    : content.praisecnt - 1,
            [praiseflg]: content.praiseflg == '00' ? '01' : '00',
        });
        giveLikeApi('33', content.id, _that.data.cityQuestionList.praiseflg);
    },

    // 操作
    editAnswer(e) {
        let answerid = e.currentTarget.dataset.answerid;
        let istop = e.currentTarget.dataset.istop;
        if (answerid) {
            if (
                app.globalData.authority == '00' ||
                app.globalData.authority == '01' ||
                app.globalData.authority == '04' ||
                app.globalData.authority == '05'
            ) {
                if (istop === '00') {
                    this.setData({
                        actions: [
                            {
                                name: '置顶',
                                color: '#333333',
                                className: 'answer-choice',
                            },
                            {
                                name: '编辑',
                                color: '#333333',
                                className: 'answer-edit',
                            },
                            {
                                name: '删除',
                                color: '#ff455e',
                                className: 'answer-delete',
                            },
                        ],
                    });
                } else {
                    this.setData({
                        actions: [
                            {
                                name: '取消置顶',
                                color: '#333333',
                                className: 'answer-choice-cancel',
                            },
                            {
                                name: '编辑',
                                color: '#333333',
                                className: 'answer-edit',
                            },
                            {
                                name: '删除',
                                color: '#ff455e',
                                className: 'answer-delete',
                            },
                        ],
                    });
                }
            } else {
                this.setData({
                    actions: [
                        {
                            name: '编辑',
                            color: '#333333',
                            className: 'answer-edit',
                        },
                        {
                            name: '删除',
                            color: '#ff455e',
                            className: 'answer-delete',
                        },
                    ],
                });
            }
        } else {
            this.setData({
                actions: [
                    {
                        name: '编辑',
                        color: '#333333',
                        className: 'question-edit',
                    },
                    {
                        name: '删除',
                        color: '#ff455e',
                        className: 'question-delete',
                    },
                ],
            });
        }
        this.setData({
            show: true,
            answerId: e.currentTarget.dataset.answerid || '',
            answerEditCont: e.currentTarget.dataset.answercontent || '',
            questionrEditCont: e.currentTarget.dataset.questioncontent || '',
            operateUid: e.currentTarget.dataset.uid,
        });
    },

    // 取消编辑
    onCancel() {
        this.setData({
            show: false,
        });
    },

    // 选中后
    onSelect(e) {
        let _that = this;
        let pages = getCurrentPages(); //当前页面栈
        let prevPage = pages[pages.length - 2]; //上一页面
        if (e.detail.className == 'answer-delete') {
            wx.showModal({
                title: '提示：',
                content: '确认要删除该项吗？',
                success: function (res) {
                    if (res.confirm) {
                        //删除回答
                        wx.request({
                            url:
                                app.globalData.rootPath +
                                `cjMap/deleteCityAnswer?authId=${app.globalData.authId}&id=${_that.data.answerId}`,
                            method: 'POST',
                            header: {
                                'content-type': 'application/json',
                            },
                            success: function (res) {
                                _that.getCityQuestionList();
                                prevPage.refresh();
                            },
                        });
                    }
                },
            });
        } else if (e.detail.className == 'answer-choice') {
            //设为优秀回答
            wx.request({
                url:
                    app.globalData.rootPath +
                    `cjMap/setCityAnswerTop?authId=${app.globalData.authId}`,
                method: 'GET',
                data: {
                    istop: '01',
                    id: _that.data.answerId, //回答id
                },
                success: function (res) {
                    _that.getCityQuestionList();
                    prevPage.refresh();
                },
            });
        } else if (e.detail.className == 'answer-edit') {
            //编辑回答
            _that.setData({
                answerEdit: true,
                showAnswerBox: true,
                answerDescription: _that.data.answerEditCont,
            });
        } else if (e.detail.className == 'answer-choice-cancel') {
            wx.request({
                url:
                    app.globalData.rootPath +
                    `cjMap/setCityAnswerTop?authId=${app.globalData.authId}`,
                method: 'GET',
                data: {
                    istop: '00', //00 普通答案 01 优秀答案
                    id: _that.data.answerId, //回答id
                },
                success: function (res) {
                    _that.getCityQuestionList();
                    prevPage.refresh();
                },
            });
        } else if (e.detail.className == 'question-edit') {
            //编辑问题
            wx.navigateTo({
                url: `../../../../releasePage/pages/addQuestion/addQuestion?id=${_that.data.id}&content=${_that.data.questionrEditCont}`,
            });
        } else if (e.detail.className == 'question-delete') {
            //删除问题
            wx.showModal({
                title: '提示：',
                content: '确认要删除该项吗？',
                success: function (res) {
                    if (res.confirm) {
                        wx.request({
                            url:
                                app.globalData.rootPath +
                                `cjMap/deleteCityQuestion?authId=${app.globalData.authId}&id=${_that.data.id}`,
                            method: 'POST',
                            header: {
                                'content-type': 'application/json',
                            },
                            success: function (res) {
                                let pages = getCurrentPages(); //当前页面栈
                                let prevPage = pages[pages.length - 2]; //上一页面
                                prevPage.refresh();
                                wx.navigateBack({
                                    detal: 1,
                                });
                                wx.showToast({
                                    title: '删除成功',
                                    icon: 'none',
                                    duration: 1500,
                                });
                            },
                        });
                    } else {
                        console.log('点击取消回调');
                    }
                    return;
                },
            });
        }
        _that.onCancel();
    },

    // 写回答弹窗
    showAnswer(e) {
        this.setData({
            showAnswerBox: true,
        });
    },
    //获取键盘高度
    getkeyHeight(e) {
        this.setData({
            answerBoxBottom: e.detail.height,
        });
    },
    // 切换匿名
    toggle() {
        this.setData({
            anonymous: !this.data.anonymous,
        });
    },
    hideCover(e) {
        this.setData({
            answerDescription: '',
            showAnswerBox: false,
            answerId: '', //回答id
        });
    },
    stopEvent(e) {
        return;
    },
    answerDescription(e) {
        this.setData({
            answerDescription: e.detail.value,
        });
        app.numLimitTip(e.detail.value.length, 500);
    },
    // 提交写回答
    submitAnswer: throttle(
        function (e) {
            let _that = this;
            if (app.visitorPublish()) return; //游客发布限制
            if (!_that.data.answerDescription) return;
            if (!_that.data.answerEdit) {
                wx.request({
                    url:
                        app.globalData.rootPath +
                        `cjMap/createCityAnswer?authId=${app.globalData.authId}`,
                    method: 'POST',
                    header: {
                        'content-type': 'application/json',
                    },
                    data: {
                        content: _that.data.answerDescription,
                        cityquestionid: _that.data.id,
                        anonymousflg: _that.data.anonymous ? '01' : '00',
                    },
                    success: function (res) {
                        if (res.data.status == 0) {
                            _that.setData({
                                answerDescription: '',
                                showAnswerBox: false,
                                answerId: '', //回答id
                                anonymous: false, //评论匿名
                            });
                            _that.getCityQuestionList();
                            wx.showToast({
                                title: '提交成功',
                                icon: 'success',
                                duration: 2000,
                            });
                            let pages = getCurrentPages(); //当前页面栈
                            let prevPage = pages[pages.length - 2]; //上一页面
                            let prevPage2 = pages[pages.length - 3];
                            prevPage.refresh();
                            prevPage2.refresh('ftPoint');
                        } else {
                            wx.showToast({
                                title: '提交失败，请重新录入或联系后台管理',
                                icon: 'none',
                                duration: 2000,
                            });
                        }
                    },
                });
            } else {
                //编辑回答
                wx.request({
                    url:
                        app.globalData.rootPath +
                        `cjMap/editCityAnswer?authId=${app.globalData.authId}`,
                    method: 'POST',
                    header: {
                        'content-type': 'application/json',
                    },
                    data: {
                        content: _that.data.answerDescription,
                        id: _that.data.answerId,
                        anonymousflg: _that.data.anonymous ? '01' : '00',
                    },
                    success: function (res) {
                        if (res.data.status == 0) {
                            _that.setData({
                                answerDescription: '',
                                showAnswerBox: false,
                                answerEdit: false,
                                answerId: '', //回答id
                                anonymous: false, //评论匿名
                            });
                            _that.getCityQuestionList();
                            wx.showToast({
                                title: '提交成功',
                                icon: 'success',
                                duration: 2000,
                            });
                            let pages = getCurrentPages(); //当前页面栈
                            let prevPage = pages[pages.length - 2]; //上一页面
                            let prevPage2 = pages[pages.length - 3]; //上一页面
                            prevPage.refresh();
                            prevPage2.refresh('ftPoint');
                        } else {
                            wx.showToast({
                                title: '提交失败，请重新录入或联系后台管理',
                                icon: 'none',
                                duration: 2000,
                            });
                        }
                    },
                });
            }
        },
        5000,
        {
            trailing: false,
        }
    ),
    // 获取有问必答详情
    getCityQuestionList() {
        let _that = this;
        let data = {
            authId: app.globalData.authId,
            id: _that.data.id,
        };
        http.getRequest(`cjMap/getCityQuestionDetail`, data).then((res) => {
            if (res.status == 0) {
                res.experience.createtime = app.timeago(
                    res.experience.createtime
                );
                for (let j = 0; j < res.experience.answerlist.length; j++) {
                    res.experience.answerlist[j].createtime = app.timeago(
                        res.experience.answerlist[j].createtime
                    );
                }
                _that.setData({
                    id: res.experience.id,
                    cityQuestionList: res.experience,
                });
            }
        });
    },
});
